package com.yuanfeng.base.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.entity.BaseSystemMessageEntity;
import com.yuanfeng.base.mapper.BaseSystemMessageMapper;
import com.yuanfeng.base.service.BaseSystemMessageService;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.dto.userms.UserInfoDTO;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @ClassName BaseSystemMessageServiceImpl
 * @Description 系统消息服务实现
 * @Author liulh
 * @Date 2023/7/19 16:02
 * @Version 1.0
 **/
@Service
public class BaseSystemMessageServiceImpl extends ServiceImpl<BaseSystemMessageMapper, BaseSystemMessageEntity>
        implements BaseSystemMessageService {

    @Override
    public ResponseResult querySysOrderNews(Map<String, Object> map) {
        String token = (String) map.get("token");
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.fail(10086, "token失效，请重新获取！");
        }
        try {
            map.put("shopId",userInfo.getShopId());
            IPage<BaseSystemMessageEntity> page = this.baseMapper.querySysOrderMessage(new Query<BaseSystemMessageEntity>().getPage(map),map);
            return ResponseResult.success(PageUtils.getPage(page));
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }

    @Override
    public ResponseResult readSysOrderNews(Map<Object, Object> map) {
        String token = (String) map.get("token");
        boolean isExistToken = RedisUtil.exists(YFConfig.YF_USER_TOKEN_USER + token);
        if (!isExistToken) {
            return ResponseResult.fail(10086, "token失效，请重新获取！");
        }
        try {
            String userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
            UserInfoDTO dto = JSON.parseObject(userInfoStr, UserInfoDTO.class);
            UserInfoVO userInfo = dto.getUser();
            String userId = userInfo.getUserId();
            BaseSystemMessageEntity baseSystemMessageEntity = new BaseSystemMessageEntity();
            baseSystemMessageEntity.setMessageIslook(1);
            UpdateWrapper<BaseSystemMessageEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("message_user_id", userId)
                    .in("message_id", (List<Integer>) map.get("messageId"));
            int isUpdateReadMessage = this.baseMapper.update(baseSystemMessageEntity, updateWrapper);
            if (isUpdateReadMessage == 0) {
                return ResponseResult.fail(2, "失败!");
            }
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }

    @Override
    @Transactional
    public ResponseResult deleteSysOrderNews(Map<Object, Object> map) {
        String token = (String) map.get("token");
        boolean isExistToken = RedisUtil.exists(YFConfig.YF_USER_TOKEN_USER + token);
        if (!isExistToken) {
            return ResponseResult.fail(10086, "token失效，请重新获取！");
        }
        try {
            String userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
            UserInfoDTO dto = JSON.parseObject(userInfoStr, UserInfoDTO.class);
            UserInfoVO userInfo = dto.getUser();
            String userId = userInfo.getUserId();
            BaseSystemMessageEntity baseSystemMessageEntity = new BaseSystemMessageEntity();
            baseSystemMessageEntity.setMessageIsdel(1);
            UpdateWrapper<BaseSystemMessageEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("message_user_id", userId)
                    .in("message_id", (List<Integer>) map.get("messageId"));
            int isdelMessage = this.baseMapper.update(baseSystemMessageEntity, updateWrapper);
            if (isdelMessage == 0) {
                return ResponseResult.fail(2, "删除失败!");
            }
            return ResponseResult.success();
        } catch (Exception e) {
            return ResponseResult.fail(500, e.getMessage());
        }
    }
}
